﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Notification.NotificationTransferRecord;
using BCCommon;
namespace BCData.OA.Notification.NotificationTransferRecord
{
    /// <summary>
    /// OA系统通知记录移交记录
    /// </summary>
	public class NotificationTransferRecordData : INotificationTransferRecordData
    {
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_NotificationTransferRecord";
        private const string QUERY = @"SELECT NotificationTransferRecordId,NotificationRecordId,ParticipantId,ParticipantName,ParticipantRole,ReceiverId,ReceiverRole,ReceiverName,
                                                TransferRemark,CreateTime 
                                        FROM OA_NotificationTransferRecord";
        
        private readonly IDatabaseContext databaseContext;
        
        public NotificationTransferRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long notificationTransferRecordId)
        {
            string sql = string.Format(@"{0} WHERE NotificationTransferRecordId=?NotificationTransferRecordId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationTransferRecordId", notificationTransferRecordId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<NotificationTransferRecordEntity> Add(NotificationTransferRecordEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_NotificationTransferRecord", new string[] { "NotificationRecordId", "ParticipantRole", "ReceiverId", "ReceiverRole", "ReceiverName", "TransferRemark" });
            sql = string.Format("{0} {1} WHERE NotificationTransferRecordId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationRecordId", entity.NotificationRecordId, DbType.Int64);
            parameters.Add("?ParticipantRole", entity.ParticipantRole, DbType.Int64);
            parameters.Add("?ReceiverId", entity.ReceiverId, DbType.Int64);
            parameters.Add("?ReceiverRole", entity.ReceiverRole, DbType.Int64);
            parameters.Add("?ReceiverName", entity.ReceiverName, DbType.Int64);
            parameters.Add("?TransferRemark", entity.TransferRemark, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<NotificationTransferRecordEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<NotificationTransferRecordEntity> Update(NotificationTransferRecordEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_NotificationTransferRecord", new string[] { "NotificationRecordId", "ParticipantId", "ParticipantName", "ParticipantRole", "ReceiverId", "ReceiverRole", "ReceiverName", "TransferRemark" }, new string[] { "NotificationTransferRecordId" });
            sql = string.Format("{0} {1} WHERE NotificationTransferRecordId=?NotificationTransferRecordId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationTransferRecordId", entity.NotificationTransferRecordId, DbType.Int64);
            parameters.Add("?NotificationRecordId", entity.NotificationRecordId, DbType.Int64);
            parameters.Add("?ParticipantId", entity.ParticipantId, DbType.Int64);
            parameters.Add("?ParticipantName", entity.ParticipantName, DbType.Int64);
            parameters.Add("?ParticipantRole", entity.ParticipantRole, DbType.Int64);
            parameters.Add("?ReceiverId", entity.ReceiverId, DbType.Int64);
            parameters.Add("?ReceiverRole", entity.ReceiverRole, DbType.Int64);
            parameters.Add("?ReceiverName", entity.ReceiverName, DbType.Int64);
            parameters.Add("?TransferRemark", entity.TransferRemark, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<NotificationTransferRecordEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long notificationTransferRecordId)
        {
            string sql = @"DELETE FROM OA_NotificationTransferRecord WHERE NotificationTransferRecordId=?NotificationTransferRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationTransferRecordId", notificationTransferRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<NotificationTransferRecordEntity> GetEntity(long notificationTransferRecordId)
        {
            string sql = string.Format(@"{0} WHERE NotificationTransferRecordId=?NotificationTransferRecordId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationTransferRecordId", notificationTransferRecordId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<NotificationTransferRecordEntity>(sql, parameters);
        }

        public async Task<IEnumerable<NotificationTransferRecordEntity>> GetList()
        {
            return await this.databaseContext.Slave.QueryAsync<NotificationTransferRecordEntity>(QUERY);
        }

        public async Task<(IEnumerable<NotificationTransferRecordEntity>, int)> Query(int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_NotificationTransferRecord {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<NotificationTransferRecordEntity>(sql, parameters);
        }
    }
}
